<template>
    <div class="warpper-box">
        <div class="inner_box">
            <form-create 
            v-model="params.fApi" 
            :rule="params.rule" 
            :option="params.option" 
            @btn-search-click="loadList(true)" 
            @btn-reset-click="reset"/>
            <PurchaseInquiryList
            :key="keys"
            :tableData="tables.tableData" 
            :tableLabel="tables.tableLabel" 
            :models="tables" 
            @on-search-click="loadList"
            @on-offer-click="function(row){return verifyClick(row,'offer')}"
            @on-details-click="function(row){return verifyClick(row,'details')}"
            @on-review-click="function(row){return verifyClick(row,'review')}"
            @on-cancelOffer-click="cancelOffer"
            @on-inquiryBidFail-click="inquiryBidFail"
            @on-publicity-click="publicity"
            @on-bidCancel-click="bidCancel"
            @on-stop-click="stopInquiry"/>

            <el-dialog :title="verify.offer.title" :visible.sync="verify.offer.visible" :width="verify.width" :close-on-click-modal="false">
              <inquirySupplierOffer v-if="verify.offer.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>

            <el-dialog :title="verify.details.title" :visible.sync="verify.details.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <inquiryDetails v-if="verify.details.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>

            <el-dialog :title="verify.review.title" :visible.sync="verify.review.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <inquiryReview v-if="verify.review.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>


        </div>
    </div>
  </template>
  
  <script>
  import { 
    PARAMS_PURCHASE_INQUIRY,
    LABEL_PURCHASE_INQUIRY,
    LABEL_PURCHASE_INQUIRY_SUPPLIER,
    OPTION_INLINE
  } from "@/views/purchase/purchaseModel/purchase_form"
  import PurchaseInquiryList from "@/views/purchase/purchaseModel/purchase_inquiry_model_list"
  import inquirySupplierOffer from "@/views/purchase/purchaseInquiry/inquiry_supplier_offer"
  import inquiryDetails from "@/views/purchase/purchaseInquiry/inquiry_details"
  import inquiryReview from "@/views/purchase/purchaseInquiry/inquiry_review"
  import { 
    bidPurchaseCommonList,
    bidPurchaseSupplierCancelQuote,
    bidPurchaseOvert,
    bidPurchaseStop,
    bidPurchaseInquiryInvitationCancel
  } from "@/api/purchase_api"
  export default {
    components:{
        PurchaseInquiryList,
        inquirySupplierOffer,
        inquiryDetails,
        inquiryReview
    },
    data() {
      return {
        keys:0,
        params:{
          fApi:{},
          rule:PARAMS_PURCHASE_INQUIRY,
          option:OPTION_INLINE
        },
        tables:{
          total:0,
          fixed:'right',
          width:'300px',
          tableData:[],
          tableLabel: []
        },
        page:{
          purchaseType:'1',
          pageNum:1,
          pageSize:10,
        },
        verify:{
          row:{},
          width:"1300px",
          offer:{
            title:'供应商报价',
            visible:false,
            hackReset:true
          },
          details:{
            title:'询价采购详情',
            visible:false,
            hackReset:true
          },
          review:{
            title:'询价采购详情',
            visible:false,
            hackReset:true
          }
        }
      };
    },
    mounted() {
      this.loadTableLalbe();
      this.$nextTick(()=>{
        this.params.fApi.setValue({"supplierName":"","name":"","time":""});
      })
    },
    methods: {
      loadTableLalbe(){
        let user = this.$store.getters.permission_user;
        if(user.purchaseSupplier !== null){//供应商角色
          this.tables.tableLabel = LABEL_PURCHASE_INQUIRY_SUPPLIER;// 列表字段动态
          this.params.fApi.hidden(true,'supplierName');//搜索条件隐藏
        }else{//其他角色
          this.tables.tableLabel = LABEL_PURCHASE_INQUIRY;// 列表字段动态
          this.params.fApi.hidden(false,'supplierName');//搜索条件隐藏
        }
        this.loadList(true);
      },
      async loadList(formData) {
        let params = this.params.fApi.bind();
        if(params.time && params.time.length != 0){
          params.startTime = params.time[0];
          params.endTime = params.time[1];
        }
        if(formData !== undefined && formData.type === 'page'){
          this.page.pageNum = formData.pageNum;
          this.page.pageSize = formData.pageSize;
        }
        if(formData===true){
          this.page.pageNum = 1;
          this.keys++;
        }
        params.projectType = 1;//1物资，2劳务
        params.bidType = this.page.purchaseType;
        params.pageNum = this.page.pageNum;
        params.pageSize = this.page.pageSize;
        const req = await bidPurchaseCommonList(params);
        if(req.code === 200){
          req.data.list.forEach(v=>{
            v.winningMoney = (v.winningMoney/100);
          })
          this.tables.tableData = req.data.list;
          let btn = this.$store.getters.permission_btns;
          let user = this.$store.getters.permission_user;
          let _supplierId = user.purchaseSupplier === null ? '' : String(user.purchaseSupplier.id);//不为空就是供应商
          /*
           1.供应商报价 3.评标中 7.中标公示 5.流标 6.终止inquiry
          */
          this.tables.tableData.forEach(v=>{
            let _supplier = v.supplierIds.split(',');
            if(v.status === "1"){//待供应商和取消报价
              if(_supplier.includes(_supplierId)){ //列表包含当前账户 存在就取消价格
                v.cancelOffer = btn.includes('inquiryBidSupplierCancelOffer');
              }else if(user.purchaseSupplier !== null){// 当前账户不为null就是供应商
                v.offer = btn.includes('inquiryBidSupplierOffer');
              }
              //终止
              v.stop = btn.includes('inquiryBidStopPurchase');
            }
            if(v.status === "3"){//评审中
              v.review = btn.includes('inquiryBidReviewPurchase');
            }
            if(v.status === '4'){//标的已公示
              if(v.gsFlag === '2' || v.gsFlag === '0'){//撤回的 未公示的 都可以继续公示
                v.publicity = btn.includes('inquiryBidBiddingPublicity');
              }else if(v.gsFlag === '1'){//已公示的继续撤回
                v.bidCancel = btn.includes('inquiryBidCancel');
              }
            }
            if(v.status === "7"){//中标公示
              v.publicity = btn.includes('inquiryBidBiddingPublicity');
            }
            if(v.status !== '5'){//不是流标状态
              v.inquiryFail = btn.includes('inquiryFailCode'); //流标
            }
           
            v.details = true;
          })
          this.tables.total = req.data.total;
        }
      },
      //终止项目
      stopInquiry(row){
        this.$confirm("终止后无法恢复，是否终止", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
           const {code} = await bidPurchaseStop(row.id);
           if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
           }
        }).catch(() => {});
      },
      //流标
      inquiryBidFail(row){
         this.$confirm("流标后无法恢复，是否流标", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          let params = {};
          params.explain = '';
          params.id = row.id;//标id
          const {code} = await bidPurchaseInquiryInvitationCancel(params);
           if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
           }
        }).catch(() => {});
       /*  this.$prompt('请输入原因', '流标', {
          confirmButtonText: '流标',
          cancelButtonText: '取消',
        }).then(async({ value }) => {
          let params = {};
          params.explain = value;
          params.id = row.id;//标id
          const {code} = await bidPurchaseInquiryInvitationCancel(params);
          if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
          }
        }).catch(() => {}); */
      },
      //取消报价
      cancelOffer(row){
        this.$confirm('确定取消报价', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
            const { code } = await bidPurchaseSupplierCancelQuote(row.id);
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(() => {});
      },
      //中标公示
      publicity(row){

        this.$confirm('确定中标公示', "提示", {
          confirmButtonText: "公示",
          cancelButtonText: "不公示",
          distinguishCancelAndClose: true,
          type: "warning",
        }).then(async () => {
            const { code } = await bidPurchaseOvert(row.id,1);
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(async (action) => {
          if(action === 'cancel'){
            const { code } = await bidPurchaseOvert(row.id,0);
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
          }
        });
      },
      //撤回中标公示
      bidCancel(row){
        this.$confirm('确定撤回公示的标', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          //1 公示，0 不公示 2撤回
            const { code } = await bidPurchaseOvert(row.id,2) ;
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(() => {});
      },
      //新增和报价
      verifyClick(row,typeVisible){
        this.verify.row = row;
        this.verify[typeVisible].hackReset = false;
        this.$nextTick(()=>{
          this.verify[typeVisible].hackReset = true;
        })
        this.verify[typeVisible].visible = true;
      },
      reset(){
        this.params.fApi.resetFields();
        this.loadList(true);
      },
      onCancel(flag){
        this.verify[flag].visible = false;
        if(flag !== 'details'){
          this.loadList(true);
        }
      }
    }
  };
  </script>